package org.dce.screencapturer.objects;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Path2D;

import org.dce.screencapturer.actions.drawing.InsertObjectAction;
import org.dce.screencapturer.util.IconFactory;

@SuppressWarnings("serial")
public class Bubble extends DotBasedShapeObject {

	private Dot top = new Dot(false,true,false,true);
	private Dot left = new Dot(true,false,true,false);
	private Dot bottom = new Dot(false,true,false,true);
	private Dot right = new Dot(true,false,true,false);
	private Dot pointer = new Dot(true,true,true,true);
	
	private final Dot[] dots = {top,left,bottom,right,pointer};

	public static final class InsertAction extends InsertObjectAction {
		public InsertAction(){
			super("actions.bubble",IconFactory.getIcon("/images/bubble.png"), null,Bubble.class);
		}
	};

	public Bubble(){
		initDots(getDots());
	}

	@Override
	protected Dot[] getDots() {
		return dots;
	}

	@Override
	protected Shape getShape() {
		
		Point leftCenter = left.getCenter();
		Point topCenter = top.getCenter();
		Point rightCenter = right.getCenter();
		Point bottomCenter = bottom.getCenter();
		Point pointerCenter = pointer.getCenter();
		
		int pointerLeftEdgetX = leftCenter.x + 10 +(int)((float)( rightCenter.x -leftCenter.x) * 0.1f);    
		int pointerRightEdgetX = leftCenter.x + 10 +(int)((float)( rightCenter.x -leftCenter.x) * 0.15f);    

		Path2D path = new Path2D.Float();
		path.moveTo(leftCenter.x+10, topCenter.y);
		path.lineTo(rightCenter.x-10, topCenter.y);
		path.curveTo(
				rightCenter.x-10, topCenter.y, 
				rightCenter.x-2, topCenter.y + 2, 
				rightCenter.x, topCenter.y + 10);
		
		path.lineTo(rightCenter.x, bottomCenter.y-10);
		
		path.curveTo(rightCenter.x, bottomCenter.y-10, 
				rightCenter.x-2, bottomCenter.y-2, 
				rightCenter.x - 10, bottomCenter.y);
		
		path.lineTo(pointerRightEdgetX, bottomCenter.y);

		path.lineTo(pointerCenter.x, pointerCenter.y);
		
		path.lineTo(pointerLeftEdgetX, bottomCenter.y);
		
		path.lineTo(leftCenter.x+10, bottomCenter.y);
		
		path.curveTo(leftCenter.x+10, bottomCenter.y, 
				leftCenter.x+2, bottomCenter.y-2, 
				leftCenter.x, bottomCenter.y-10);
		
		path.lineTo(leftCenter.x, topCenter.y+10);

		path.curveTo(leftCenter.x,topCenter.y+10,
				leftCenter.x+2,topCenter.y+2,
				leftCenter.x+10,topCenter.y);
		return path;
	}
	
	@Override
	protected Rectangle calculateInnerBounds(Shape shape) {
		return new Rectangle(
			left.getLeftX(),
			top.getBottomY(),
			right.getX()-left.getLeftX(),
			bottom.getY()-top.getBottomY());
	}

	@Override
	protected void onInsertionProgress() {
		
		Rectangle r = getBounds(getInsertionFirstPoint(), getInsertionSecondPoint());
		
		setLocation(r.getLocation());
		
		right.setLocation(r.width-right.getWidth(), r.height/2);
		left.setLocation(0,r.height/2);
		top.setLocation(r.width/2,0);
		bottom.setLocation(r.width/2,r.height-40);
		pointer.setLocation(40,r.height-pointer.getHeight());
		
	}

	@Override
	protected void onInsertionDone() {
		
		Rectangle r = getBounds(getInsertionFirstPoint(), getInsertionSecondPoint());

		setLocation(r.getLocation());

		right.setLocation(r.width-right.getWidth(), r.height/2);
		left.setLocation(0,r.height/2);
		top.setLocation(r.width/2,0);
		bottom.setLocation(r.width/2,r.height-40);
		pointer.setLocation(40,r.height-pointer.getHeight());
		
	}
	
	@Override
	protected void notifyDotMoved(Dot dot) {
		
		if (dot == right || dot == left){
			
			int centerX = left.getCenter().x + (right.getCenter().x - left.getCenter().x)/2;
			
			bottom.setLocation(centerX,bottom.getY());
			top.setLocation(centerX,top.getY());
			
		} else if (dot == top || dot == bottom){
			
			int centerY = top.getCenter().y + (bottom.getCenter().y - top.getCenter().y)/2;
			
			left.setLocation(left.getX(),centerY);
			right.setLocation(right.getX(),centerY);
		}
		
		super.notifyDotMoved(dot);
	}

}
